/* authentication related services */

var express = require('express')
var router = express.Router(),
    rp = require('request-promise'),
    template = require("es6-template-strings"),
    debug = require('debug')('react-demo')

var configFile = 'config.'+process.env.NODE_ENV+'.json'
var config = require('../../config/'+configFile);
//var App = React.createFactory(main.App);

router.post("/", function(req, res, next){
  var token = req.cookies.access_token;
  console.log(token)
  var option = {
    method: 'GET',
    uri: config.apiHost+'/wxapi/menus?app_id='+req.cookies.appId,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }
  rp(option)
    .then(function(result) {
      debug(result);
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

router.post("/create", function(req, res, next) {
  var token = req.cookies.access_token;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/wxapi/menus?app_id='+req.cookies.appId,
    body: req.body,
    json: true,
    headers: {
      authorization: 'Bearer '+token
    }
  }

  rp(option)
    .then(function(result) {
      debug(result)
      if (result.errcode == 0) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

router.post("/reset", function(req, res, next) {
  var token = req.cookies.access_token;
  var appId = req.cookies.appId;

  //todo: 小区介绍的media_id
  var menuData = {
   "menu":{
      "button":[
         {
            "name":"小区互动",
            "sub_button":[
              {
                 "type":"click",
                 "name":"小区介绍",
                 "key": "contactus"
              },
               {
                  "type":"view",
                  "name":"小区公告",
                  "url":"${host}/wechat/billboard?appId=${appId}",
                  "sub_button":[

                  ]
               },
               {
                  "type":"view",
                  "name":"小区活动",
                  "url":"${host}/wechat/activity?appId=${appId}",
                  "sub_button":[

                  ]
               },
               {
                  "type":"view",
                  "name":"意见反馈",
                  "url":"${host}/wechat/see_suggestion?appId=${appId}",
                  "sub_button":[

                  ]
               }
            ]
         },
         {
            "name":"物业服务",
            "sub_button":[
               {
                  "type":"view",
                  "name":"我要报修",
                  "url":"${host}/wechat/function_yetOpen?appId=${appId}",
                  "sub_button":[

                  ]
               },
               {
                  "type":"view",
                  "name":"物业缴费",
                  "url":"${host}/wechat/my_bind?appId=${appId}&mode=15",
                  "sub_button":[

                  ]
               },
               {
                  "type":"view",
                  "name":"办事指南",
                  "url":"${host}/wechat/guide?appId=${appId}",
                  "sub_button":[

                  ]
               },
              {
                  "type":"click",
                  "name":"联系我们",
                  "key": "contactus"
               }
            ]
         },
         {
            "name":"更多服务",
            "sub_button":[
               {
                  "type":"view",
                  "name":"我的单元",
                  "url":"${host}/wechat/my_bind?appId=${appId}&mode=0",
                  "sub_button":[

                  ]
               },
               {
                  "type":"view",
                  "name":"报修查询",
                  "url":"${host}/wechat/function_yetOpen?appId=${appId}",
                  "sub_button":[

                  ]
               }
            ]
         }
      ]
   }
  }


  var templateData = JSON.stringify(menuData);
  var formatData = template(templateData,
                            {appId: appId,
                            host: config.wechatHost});
  menuData = JSON.parse(formatData);
  var option = {
    method: 'POST',
    uri: config.apiHost+'/wxapi/menus?app_id='+req.cookies.appId,
    body: menuData.menu,
    json: true,
    headers: {
      authorization: 'Bearer '+token
    }
  }

  rp(option)
    .then(function(result) {
      debug(result)
      if (result.errcode == 0) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })

})

module.exports = router;
